<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="商店">
          <a-select
            showSearch
            :defaultActiveFirstOption="false"
            :showArrow="false"
            :filterOption="false"
            placeholder="请选择商店"
            @search="handleShopSearch"
            @change="handleShopChange"
            notFoundContent="null"
            v-decorator="['shopId', { rules: [{ required: true, message: '请选择商店' }] ,initialValue :''}]"
          >
            <a-select-option v-for="d in shopList" :key="d.id">{{ d.shopName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型">
          <a-radio-group v-decorator="['type', validatorRules.type]">
            <a-radio-button value="0">智能推荐</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group v-decorator="['status', validatorRules.status]">
            <a-radio-button value="0">开启</a-radio-button>
            <a-radio-button value="1">关闭</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
          <a-input-number v-decorator="['sort', validatorRules.sort]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'ShopRecommondModal',
  data() {
    return {
      shopName: '',
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      shopList: [],
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        type: { rules: [{ required: true, message: '请输入类型  !' }] },
        status: { rules: [{ required: true, message: '请选择状态  !' }], initialValue: '0' },
        sort: { rules: [{ required: true, message: '请输入排序 !' }], initialValue: 0 }
      },
      url: {
        add: '/shoprecommond/add',
        edit: '/shoprecommond/edit'
      }
    }
  },
  created() {},
  methods: {
    add() {
      this.form.resetFields()
      this.visible = true
    
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'shopId', 'type', 'status', 'sort'))
        this.shopName=this.model.shopName
        let params = { id: this.model.shopId }
        this.getShopList(params)
        //时间格式化
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleShopChange(value) {
      for (let i in this.shopList) {
        console.log(this.shopList[i])
        if (this.shopList[i].id == value) {
          this.shopName = this.shopList[i].shopName
        }
      }
    },
    handleShopSearch(value) {
      value = value + '*'
      let params = { shopName: value }
      this.getShopList(params)
    },
    getShopList(params) {
      getAction('/shop/web/shopList', params).then(res => {
        if (res.success) {
          this.shopList = res.result.records
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化
          formData.shopName = this.shopName
          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style lang="less" scoped></style>
